package com.osdp.sc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.common.util.ConvertUtil;
import com.osdp.sc.entity.ScRolePosition;
import com.osdp.sc.mapper.ScRolePositionMapper;
import com.osdp.sc.service.IScRolePositionService;
import com.osdp.sc.uitl.Fields;
import com.osdp.sc.vo.ScRolePositionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScRolePositionServiceImpl extends ServiceImpl<ScRolePositionMapper, ScRolePosition> implements IScRolePositionService {

    @Override
    public List<ScRolePositionVo> queryPositionRoleByPositionId(String positionId) {
        List<ScRolePosition>  userRoleList = super.list(new QueryWrapper<ScRolePosition>()
                .eq(StringUtils.isNotBlank(positionId),Fields.ScRolePositionQ.positionId, positionId));
        return ConvertUtil.sourceToTarget(userRoleList, ScRolePositionVo.class);
    }

    @Override
    public List<String> queryRoleIdByPositionId(String positionId) {
        List<ScRolePositionVo> list = queryPositionRoleByPositionId(positionId);
        return list.stream().map(ScRolePositionVo::getRoleId).collect(Collectors.toList());
    }

    @Override
    @Transactional
    public boolean batachSaveOrUpdatePositionRoleByPositionId(String positionId, List<String> roleIds) {
        this.deleteByPositionId(positionId);//直接先删除，再增加
        if(!CollectionUtils.isEmpty(roleIds)){
            //List<UcUserRole>  userRoleList =ConvertUtil.sourceToTarget(userRoleVoList, UcUserRole.class);
            List<ScRolePosition> positionRoleList = new LinkedList<>();
            for(String id : roleIds){
                ScRolePosition entity = new ScRolePosition();
                entity.setRoleId(id);
                entity.setPositionId(positionId);
                positionRoleList.add(entity);
            }
            return super.saveBatch(positionRoleList);
        }
        return true;
    }

    @Override
    public boolean deleteByPositionId(String positionId) {
        if(StringUtils.isNotBlank(positionId)) {
            return super.remove(new QueryWrapper<ScRolePosition>()
                    .eq(Fields.ScRolePositionQ.positionId, positionId));
        }
        return false;
    }

    @Override
    public boolean deleteByRoleId(String roleId) {
        return super.remove(new QueryWrapper<ScRolePosition>()
                .eq(StringUtils.isNotBlank(roleId), Fields.ScRolePositionQ.roleId, roleId));
    }

    @Override
    public List<ScRolePositionVo> queryPositionRoleByRoleId(String roleId) {
        List<ScRolePosition>  userRoleList = super.list(new QueryWrapper<ScRolePosition>()
                .eq(StringUtils.isNotBlank(roleId),Fields.ScRolePositionQ.roleId, roleId));
        return ConvertUtil.sourceToTarget(userRoleList, ScRolePositionVo.class);
    }

    @Override
    @Transactional
    public boolean batachSaveOrUpdatePositionRoleByRoleId(String roleId, List<String> positionIds) {
        this.deleteByRoleId(roleId);//直接先删除，再增加
        boolean isSuccess = true;
        if(!CollectionUtils.isEmpty(positionIds)){
            List<ScRolePosition> positionRoleList = new LinkedList<>();
            for(String id : positionIds){
                ScRolePosition entity = new ScRolePosition();
                entity.setRoleId(roleId);
                entity.setPositionId(id);
                positionRoleList.add(entity);
            }
            isSuccess =  super.saveBatch(positionRoleList);
        }
        return isSuccess;
    }
}
